package com.kele.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kele.domain.Blog;
import com.kele.domain.User;
import com.kele.domain.pageInfo;
import com.kele.service.UserService;
import com.kele.utils.PageUtil;
import com.kele.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 赵奥林
 * @date 2021年05月27日 10:04
 * @Description  管理员模块：对用户的操作模块
 *
 */
@Controller
@RequestMapping("/manager")
@Api(value = "管理员模块",tags = {"管理员操作用户接口"})
public class ManagerController {


    @Resource
    private UserService userService;


    /**
     * 管理员管理所有用户信息
     * @param model 传递数据
     * @param pageNum 需要的页数
     * @param dataNums 每页的数据
     * @param pageBarSize 几个按钮
     * @return 页面
     */
    @RequestMapping(value = "/findAllUser",method = RequestMethod.GET)
    public String findAllUser(Model model,
                              @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                              @RequestParam(value = "dataNums",defaultValue = "5") int dataNums,
                              @RequestParam(value = "pageBarSize",defaultValue = "5")int pageBarSize,
                              @RequestParam(value = "flag",defaultValue = "-1") int flag){
        int count =userService.findAllSize("user",flag);
        PageUtil pageUtil=new PageUtil(pageNum,count,dataNums);
        pageNum=pageUtil.getPageNum();
        int start= pageUtil.getStart();
        List<User> list =userService.findAll(start,dataNums);
        pageInfo pageInfo=new pageInfo(list,pageNum,count,dataNums,pageBarSize);

        model.addAttribute("pageInfo",pageInfo);

        return "static_resources/templates/management";
    }


    /**
      *@author 赵奥林
      *@description 2021/5/27 23:22 //跳转到修改页面
     * @param id
     * @param model
     * @return java.lang.String
      */
    @RequestMapping(value = "/toUpdateUsers",method = RequestMethod.GET)
    public String toUpdateUsers(int id,Model model){
        User user = userService.queryUserById(id);
        model.addAttribute("user",user);
        return "/static_resources/templates/updateUsers";
    }

    /**
      *@author 赵奥林
      *@description 2021/5/27 23:22 //管理员修改用户信息
     * @param user
     * @return java.lang.String
      */
    @RequestMapping(value = "/updateUsers",method = RequestMethod.POST)
    public String updateUsers(User user,int pageNum){
       userService.updateUser(user);
        System.out.println(user);
        System.out.println(pageNum);
       return "redirect:/manager/findAllUser?pageNum="+pageNum;
    }



    /**
      *@author 赵奥林
      *@description 2021/5/27 23:22 //管理员删除用户
     * @param id
     * @return java.lang.String
      */
    @RequestMapping(value = "/deleteUser",method = RequestMethod.GET)
    public String deleteUser(int id,int pageNum,int pageSize){
       userService.deleteUser(id);
        System.out.println(pageNum);
        System.out.println(pageSize);
        if (pageSize-1<=0){
            pageNum=pageNum-1;
        }
        if (pageNum-1<=0){
            pageNum=1;
        }
       return "redirect:/manager/findAllUser?pageNum="+pageNum;
    }

    /**
     * 批量删除用户
     * @param
     * @return
     */
    @RequestMapping(value = "/deleteList",method = RequestMethod.DELETE)
    @ApiOperation(value = "批量删除用户",notes = "根据id集合删除用户")
    @ResponseBody
    @ApiImplicitParams(
            @ApiImplicitParam(value = "id数组",name = "idArr",required = true)
    )
    public ResponseData deleteList(@RequestParam(value = "idArr")Integer[] ids){
        userService.deleteList(ids);
        return ResponseData.success("删除成功");
    }

    /**
     * 后台用户管理模糊查询
     * @param attribute
     * @param model
     * @return
     */
    @RequestMapping(value = "/fuzzy",method = RequestMethod.GET)
    @ApiOperation(value = "后台用户管理模糊查询",notes = "根据用户属性模糊查询用户")
    public String fuzzy(String attribute,Model model){
        List<User> fuzzy = userService.fuzzy(attribute);
        if (fuzzy.size()!=0){
            model.addAttribute("fuzzy",fuzzy);
            return "/static_resources/templates/management";
        }else{
            return "/static_resources/templates/management";
        }

    }
}
